home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: More Modulus questions
- Date: 21 Feb 1996 14:02:04 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gg4osINN9ku@keats.ugrad.cs.ubc.ca>
- References: <Pine.SOL.3.90.960219171637.21117B-100000@eddie> <4gfnka$ni7@spanky.pls.ov.com> <4gfp5a$r8e@cloner2.ix.netcom.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4gfp5a$r8e@cloner2.ix.netcom.com>, KPN <wzjn@ix.netcom.com> wrote:
- >Good Afternoon,
- >
- >Recently I asked a question concerning the proper use of the Modulus
- >code. Many replied back to me to help clear up m confusion, and many
- >replied back to the group to help me. I thank all of you for your time
- >and expertise.
- >
- >As I looked over what had been sent to me, a great deal of the
- >responses helped by pointing out that what I was doing was incorrect -
- >however, no one really showed me what I should be doing to correct the
- >problem! Well, actually, a few examples were to be had, but were so far
- >over my knowledge of C thus far, as to be useless to me. Thank you
- >anyway to those who sent in.
- >
- >Here again is my problem: (From æHow to Program CÆ second edition
- >Deitel/Dietel - Write a program that reads an integer and determines,
- >and prints how many digits in the integer are 7Æs)
- >
- >#include <stdio.h>
- >
- >main()
- >{
- > int number;
- > int first, second, third, fourth;
- > int integer1, integer2, integer3, integer4;
- >
- > printf("Enter a four digit number: ");
-
- Should say "Enter a _non_negative_ four digit number".
- You should be using unsigned math. The modulus operator has
- implementation-defined results for negative operands.
-
- > scanf("%d", &number);
-
- Has to be fixed to %u once you convert to unsigned.
-
- >/*
- > Split the four digit number into four seperate integers
- >*/
- >
- > first = number / 1000;
- > integer1 = number % 1000;
-
- why not use an accumulator?
- /* number is 0-9999 */
- num[0] = number / 1000; /* therefore num[0] is 0-9 */
- residue = number % 1000; /* residue is 0-999 */
-
- num[1] = residue / 100; /* therefore num[1] is 0-9 */
- residue %= 100; /* residue is 0-99 */
-
- num[2] = residue / 100; /* therefore num[2] is 0-9 */
-
- num[3] = residue % 10; /* and num[3] is 0-9 */
-
- > second = integer1 / 100;
- > integer2 = integer1 % 100;
- >
- > third = integer2 / 10;
- > integer3 = integer2 % 10;
- >
- > fourth = integer3 / 1;
- > integer4 = integer3 % 1;
-
- Dividing by 1 is a waste. The remainder of anything divided by 1 is zero.
- Think: how could there be a residue in integer4? You have completely
- determined all four digits of the integer which uniquely identify it. There
- can't be anything left over. See my above code.
-
- >/*
- > Use modulas to determine if any integers are a 7
- >*/
- >
- > if (first % 7)
- > printf("First integer was a 7\n");
- > else
- > printf("Not a 7\n");
-
- This is silly. You obviously did NOT read the replies to your posting.
- The four integers are guaranteed to be in the range 0-9 (assuming that a
- positive integer was input). Are you completely oblivious to the existence of
- the '==' operator? Why are you using a relatively expensive modulus operation
- in place of a comparison? Maybe you should focus on mastering the use of
- frequently used operators.
-
- if (first == 7)
- printf("First integer is a 7\n");
-
- OR
-
- puts((first == 7) ? "First digit is a 7\n" : "First digit is not 7\n");
-
- Your use of '%' is wrong here even as a test test for numbers that are
- divisible by 7. If "first" is actually equal to 7, then the expression
- "first % 7" evaluates to zero, since 7 divides itself evenly. Hence your
- message will falsely report that the integer is _NOT_ 7.
- --
-
-